package bb.lanxing.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.NumberPicker;
import androidx.core.content.ContextCompat;
import bb.lanxing.R;
import bb.lanxing.model.LevelDataItem;
import bb.lanxing.util.DensityUtil;
import bb.lanxing.util.text.TextUtils;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AreaPicker extends NumberPicker implements NumberPicker.OnValueChangeListener {
    private OnAreaChangeListener changeListener;
    private String initialArea;
    private List<LevelDataItem> levelDataItemList;

    public interface OnAreaChangeListener {
        void onArea(LevelDataItem levelDataItem);
    }

    public AreaPicker(Context context) {
        super(context);
    }

    public AreaPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AreaPicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public AreaPicker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        setNumberPickerDividerColor();
        setNumberPickerDividerHeight();
        setOnValueChangedListener(this);
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        if (child instanceof EditText) {
            ((EditText) child).setTextColor(ContextCompat.getColor(getContext(), R.color.grey_333333));
            child.setEnabled(false);
            child.setFocusable(false);
            child.setFocusableInTouchMode(false);
        }
    }

    public void setNumberPickerDividerHeight() {
        Field[] declaredFields;
        for (Field field : NumberPicker.class.getDeclaredFields()) {
            if (field.getName().equals("mSelectionDividerHeight")) {
                field.setAccessible(true);
                try {
                    field.set(this, DensityUtil.dp2px(getContext(), 1.0f));
                    return;
                } catch (Resources.NotFoundException | IllegalAccessException |
                         IllegalArgumentException e) {
                    e.printStackTrace();
                    return;
                }
            }
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    public void setNumberPickerDividerColor() {
        Field[] declaredFields;
        for (Field field : NumberPicker.class.getDeclaredFields()) {
            if (field.getName().equals("mSelectionDivider")) {
                field.setAccessible(true);
                try {
                    field.set(this, new ColorDrawable(ContextCompat.getColor(getContext(), R.color.sprint_watchface_data_field_divider)));
                    return;
                } catch (Resources.NotFoundException | IllegalAccessException |
                         IllegalArgumentException e) {
                    e.printStackTrace();
                    return;
                }
            }
        }
    }

    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        LevelDataItem levelDataItem = this.levelDataItemList.get(newVal);
        OnAreaChangeListener onAreaChangeListener = this.changeListener;
        if (onAreaChangeListener != null) {
            onAreaChangeListener.onArea(levelDataItem);
        }
    }

    public LevelDataItem getArea() {
        int value;
        List<LevelDataItem> list = this.levelDataItemList;
        if (list == null || list.isEmpty() || (value = getValue()) < 0 || value >= this.levelDataItemList.size()) {
            return null;
        }
        return this.levelDataItemList.get(value);
    }

    public AreaPicker init(List<LevelDataItem> itemList) {
        if (itemList == null) {
            itemList = new ArrayList<>(Collections.emptyList());
        }
        setDisplayedValues(null);
        setMaxValue(Math.max(0, itemList.size() - 1));
        setMinValue(0);
        int size = itemList.size();
        String[] strArr = new String[size];
        if (itemList.isEmpty()) {
            strArr = new String[]{""};
        } else {
            for (int i = 0; i < size; i++) {
                strArr[i] = itemList.get(i).getTitle();
            }
        }
        setDisplayedValues(strArr);
        this.levelDataItemList = itemList;
        if (!TextUtils.isEmptyOrNull(this.initialArea)) {
            if (!areaInner(this.initialArea)) {
                area(strArr[0]);
            }
            this.initialArea = null;
        } else {
            area(strArr[0]);
        }
        return this;
    }

    public AreaPicker initialArea(String initialArea) {
        this.initialArea = initialArea;
        return this;
    }

    public AreaPicker area(String title) {
        areaInner(title);
        return this;
    }

    private boolean areaInner(String title) {
        List<LevelDataItem> list = this.levelDataItemList;
        if (list != null && !list.isEmpty()) {
            for (int i = 0; i < this.levelDataItemList.size(); i++) {
                LevelDataItem levelDataItem = this.levelDataItemList.get(i);
                if (levelDataItem.getTitle().equals(title)) {
                    setValue(i);
                    OnAreaChangeListener onAreaChangeListener = this.changeListener;
                    if (onAreaChangeListener != null) {
                        onAreaChangeListener.onArea(levelDataItem);
                        return true;
                    }
                    return true;
                }
            }
        }
        return false;
    }

    public AreaPicker areaListener(OnAreaChangeListener listener) {
        this.changeListener = listener;
        return this;
    }

    public AreaPicker parent(AreaPicker picker) {
        picker.areaListener(new OnAreaChangeListener() {
            @Override
            public void onArea(LevelDataItem levelDataItem) {
                AreaPicker.this.init(levelDataItem.getChildren());
            }
        });
        return this;
    }
}
